<?php
/**
 * app端登陆参数
 * User: Administrator
 * Date: 2017/8/14
 * Time: 11:26
 */

defined('InNcPHP') or exit('Access Invalid!');

class  loginControl  extends  Control{


    public function __construct()
    {

            //$this->_checkApiSign();
    }

    public function indexOp(){

        $url = 'http://a.app.qq.com/o/simple.jsp?pkgname=org.soshow.liupanshui';
        redirect($url);
        //echo 'login';
    }

    public  function  accept_app_paramOp(){

        $member_model = Model('member');
        $unionid = $_POST['unionid'];
        $member = $member_model->getMemberInfo(array('unionid'=>$unionid));

        if($member){
            //老用户登陆
            //生成token
            $token = $this->_get_token($member['member_id'],$member['nickname']);
            echo json_encode(array('code'=>2000,'data'=>array('token'=>$token),'message'=>'登陆成功'));exit;
        }else{
            //新用户写入数据库
            $result = array();
            $result['openid'] = $_POST['openid'];
            $result['nickname'] = $_POST['nickname'];
            $result['sex'] = $_POST['sex'];
            $result['headimgurl'] = $_POST['headimgurl'];
            $result['province'] = $_POST['province'];//省
            $result['city'] = $_POST['city'];
            $result['unionid'] = $_POST['unionid'];
            $result['addtime'] = time();
            $result['is_merchant'] = 0;
            $result['belong_proxy'] = null;
            $res = $member_model->addmember($result);
            if($res){
                $token = $this->_get_token($res,$_POST['nickname']);//生成token
                echo json_encode(array('code'=>2000,'data'=>array('token'=>$token),'message'=>'注册成功'));exit;
            }

        }

    }

    /**
     * 登陆生成token
     */
    private function _get_token($member_id, $member_name) {
        $model_mb_user_token = Model('mb_user_token');

        //重新登陆后以前的令牌失效
        //暂时停用
        $condition = array();
        $condition['member_id'] = $member_id;
        $condition['client_type'] = 'app';
        $model_mb_user_token->delMbUserToken($condition);

        //生成新的token
        $mb_user_token_info = array();
        $token = md5($member_name . strval(TIMESTAMP) . strval(rand(0,999999)));
        $mb_user_token_info['member_id'] = $member_id;
        $mb_user_token_info['member_name'] = $member_name;
        $mb_user_token_info['token'] = $token;
        $mb_user_token_info['login_time'] = TIMESTAMP;
        $mb_user_token_info['client_type'] = 'app';
        $result = $model_mb_user_token->addMbUserToken($mb_user_token_info);
        if($result) {
            return $token;
        } else {
            return null;
        }

    }

    // 接口权限判断
    private function _checkApiSign(){

        $sign = $_POST['sign'];

        //签名
        $sign_str      =$_POST['openid'];

        if(empty($_POST['openid'])){
            echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'参数错误'));exit;
        }

        if($sign!=md5($sign_str.'lvbang')){
            echo json_encode(array('code'=>4000,'data'=>(object)array(),'message'=>'签名错误'));exit;
        }

    }


 }